package co.yixiang.modules.shop.rest;

import co.yixiang.aop.log.Log;
import co.yixiang.constant.ShopConstants;
import co.yixiang.modules.shop.domain.YxStoreBrand;
import co.yixiang.modules.shop.service.YxStoreBrandService;
import co.yixiang.modules.shop.service.dto.YxStoreBrandQueryCriteria;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @author frq
* @date 2020-07-10
*/
@Api(tags = "品牌管理")
@RestController
@RequestMapping("/api/yxStoreBrand")
public class YxStoreBrandController {

    private final YxStoreBrandService yxStoreBrandService;

    public YxStoreBrandController(YxStoreBrandService yxStoreBrandService) {
        this.yxStoreBrandService = yxStoreBrandService;
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
//    @PreAuthorize("@el.check('yxStoreBrand:list')")
    public void download(HttpServletResponse response, YxStoreBrandQueryCriteria criteria) throws IOException {
        yxStoreBrandService.download(yxStoreBrandService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询品牌")
    @ApiOperation("查询品牌")
//    @PreAuthorize("@el.check('yxStoreBrand:list')")
    public ResponseEntity<Object> getYxStoreBrands(YxStoreBrandQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(yxStoreBrandService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增品牌")
    @ApiOperation("新增品牌")
//    @PreAuthorize("@el.check('yxStoreBrand:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody YxStoreBrand resources){
        return new ResponseEntity<>(yxStoreBrandService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改品牌")
    @ApiOperation("修改品牌")
//    @PreAuthorize("@el.check('yxStoreBrand:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody YxStoreBrand resources){
        yxStoreBrandService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除品牌")
    @ApiOperation("删除品牌")
//    @PreAuthorize("@el.check('yxStoreBrand:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Integer[] ids) {
        yxStoreBrandService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
    @ApiOperation(value = "是否推荐")
    @PostMapping(value = "/onsale/{id}")
    public ResponseEntity onSale(@PathVariable Integer id, @RequestBody String jsonStr) {
        JSONObject jsonObject = JSON.parseObject(jsonStr);
        int isShow = Integer.valueOf(jsonObject.get("isShow").toString());
        yxStoreBrandService.onSale(id, isShow);
        return new ResponseEntity(HttpStatus.OK);
    }
}